package cn.itcast.solr;

import cn.itcast.solr.pojo.Item;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.util.List;

public class SolrJQuery {

    @Test
    public void testQuery() throws SolrServerException {
        // 创建solr服务对象, solr服务器的地址+core名称
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        // 初始化查询对象
        SolrQuery query = new SolrQuery("title:手机");
        // 执行查询获取结果集
        QueryResponse response = solrServer.query(query);
        // 解析响应结果集
        SolrDocumentList documents = response.getResults();

        // 获取命中条数
        System.out.println("一共命中" + documents.getNumFound() + "条！");
        for (SolrDocument document : documents) {
            System.out.println("id: " + document.get("id"));
            System.out.println("title: " + document.get("title"));
            System.out.println("price: " + document.get("price"));
        }
    }

    @Test
    public void testQueryBean() throws SolrServerException {
        // 创建solr服务对象, solr服务器的地址+core名称
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        // 初始化查询对象
        SolrQuery query = new SolrQuery("title:手机");
        // 执行查询获取结果集
        QueryResponse response = solrServer.query(query);
        // 解析响应结果集
        List<Item> items = response.getBeans(Item.class);

        System.out.println("一共命中" + response.getResults().getNumFound() + "条");

        for (Item item : items) {
            System.out.println(item);
        }
    }

    @Test
    public void testBoolBean() throws SolrServerException {
        // 创建solr服务对象, solr服务器的地址+core名称
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        // 初始化查询对象
        SolrQuery query = new SolrQuery("(id:2 OR id:3) AND (id:5 OR id:2)");
        // 执行查询获取结果集
        QueryResponse response = solrServer.query(query);
        // 解析响应结果集
        List<Item> items = response.getBeans(Item.class);

        System.out.println("一共命中" + response.getResults().getNumFound() + "条");

        for (Item item : items) {
            System.out.println(item);
        }
    }

    @Test
    public void testFuzzyBean() throws SolrServerException {
        // 创建solr服务对象, solr服务器的地址+core名称
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        // 初始化查询对象
        SolrQuery query = new SolrQuery("id:[1 TO 2]");
        // 执行查询获取结果集
        QueryResponse response = solrServer.query(query);
        // 解析响应结果集
        List<Item> items = response.getBeans(Item.class);

        System.out.println("一共命中" + response.getResults().getNumFound() + "条");

        for (Item item : items) {
            System.out.println(item);
        }
    }
}
